﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    class AB55Report : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------

        string strCompany = "HLT";
        string strExcelFile = "";
        string sheetName = "";
        string Standard = "AB55";
        string filePath = @"C:\intex.w\Report\HLT\HLTStandard.xls";
        string KRS, LFD, FACTORY, STAFF;
        string DocType;
        string CellName = "";
        string CellValue = "";
        #endregion
        public AB55Report()
            : base()
        {
        }
        public AB55Report(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            KRS = dicParamater["KRS"].ToString().Trim();
            LFD = dicParamater["LFD"].ToString().Trim();
            FACTORY = dicParamater["FACTORY"].ToString().Trim();
            STAFF = dicParamater["STAFF"].ToString().Trim();
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            strExcelFile = @"HLT\HLTStandard.xls";
            #region Excel  columns
            columns.Add("SORDERNO");
            columns.Add("ARTICLE");
            columns.Add("RATE");
            columns.Add("FDISM");
            columns.Add("PRICE");
            columns.Add("SMTR");
            columns.Add("SPCNUM");
            columns.Add("SQTY");
            columns.Add("ABPENDDAT");
            columns.Add("SFACTORY");
            #endregion
        }


        public void FillInExcelData(ArrayList columns, DataSet setSoruce, int xStart = 2, int yStart = 1)
        {
            try
            {
                string DAT = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString()
                    + "月" + DateTime.Now.Day.ToString() + "日";
                exAgent.SetCellValue(2, 3, FACTORY);
                exAgent.SetCellValue(2, 5, STAFF);
                exAgent.SetCellValue(23, 12, DAT);
                int x = xStart, y = yStart;
                DataTable dtSoruce = setSoruce.Tables["ABPOS"];
                foreach (DataRow dataRow in dtSoruce.Rows)
                {
                    y = yStart;
                    foreach (string column in columns)
                    {
                        exAgent.SetCellValue(x, y, dataRow[column].ToString());
                        y++;
                    }
                    x++;
                }
                HideNullRows();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void HideNullRows()
        {
            try
            {
                int HidedRows = 0;
                int rowstart = 1;
                int rowend = 1;
                int col1, col2, col3;

                var val1 = exAgent.GetCellValue(1, 1);

                rowstart = 5; rowend = 14; col1 = 7; col2 = 8;
                for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                {
                    val1 = exAgent.GetRangeValue(Rowi, Rowi, col1, col2).Trim();
                    if ((string)val1 == "0")
                    {
                        HidedRows++;
                        exAgent.HideRows(Rowi, col1, Rowi, col2);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void WriteExcelReport(string fileType)
        {
            string filename = "";
            string sheetName = "";
            string Standard = "AB55";
            strExcelFile = @"HLT\HLTStandard.xls";
            string filePath = @"C:\intex.w\Report\" + strExcelFile;
            exAgent.Open(filePath);
            exAgent.Company = strCompany;
            exAgent.strExcelFile = strExcelFile;
            string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
            filename = @"询货期" + KRS + "_" + LFD + ".xls";
            sheetName = filedate + "_" + DateTime.Now.ToShortTimeString().Replace(@":", "");
            exAgent.CreateNewSheet(filename, Standard, sheetName);
            FillInExcelData(columns, dsSource, 5, 2);
            base.WriteExcelReport(fileType);
        }



    }
}